<?php

    class Group_attributes_model extends MY_Model
    {
        function __construct()
        {
            parent::__construct();
            $this->table = 'group_attributes';
        }

        function get_attributes_on_group($group_id,$custom=NULL,$active=TRUE,$required=NULL)
        {
            $q = "
            SELECT 
            group_attributes.attributes_ID as attribute_ID,
            attributes.name as attribute_name,
            attributes.type as attribute_type,
            attributes.required as attribute_required,
            attributes.custom as attribute_custom,
            attributes.sort as attribute_sort
            FROM group_attributes
            LEFT JOIN attributes ON group_attributes.attributes_ID = attributes.ID
            WHERE group_attributes.groups_ID = $group_id
            AND group_attributes.active = $active
            ";

            if(isset($custom)) $q.=" AND attributes.custom = 1";
            if(isset($required)) $q.=" AND attributes.required = 1";

            $q.= " ORDER BY group_attributes.sort ASC";

            $res = $this->db->query($q)->result_array();
            return $res;
        }

        function set_active_group_attribute($group_id,$attribute_id,$active)
        {
            $this->db->where('groups_ID', $group_id);
            $this->db->where('attributes_ID', $attribute_id);
            $this->db->update('group_attributes', array('active'=>$active));
        }
    }

?>
